package site.marqstree.latte.ec.launch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.core.app.AccountManager;
import site.marqstree.latte.core.app.IUserChecker;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.core.storage.LattePreference;
import site.marqstree.latte.ui.launch.LauncherHolderCreator;
import site.marqstree.latte.ui.launch.ILauncherListener;
import site.marqstree.latte.ui.launch.OnLauncherFinishTag;
import site.marqstree.latte.ui.launch.ScrollLauncherTag;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import java.util.ArrayList;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.launch
 * 文件名: LauncherScrollDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/18 12:20
 * 描述: 轮播启动页
 */
public class LauncherScrollDelegate extends LatteDelegate implements OnItemClickListener {

    //轮播图控件
    private ConvenientBanner<Integer> mConvenientBanner = null;
    //本地图片资源ID数组
    private static final ArrayList<Integer> LAUNCH_IMAGES = new ArrayList<>();

    private ILauncherListener mILauncherListener = null;

    //初始化轮播图
    private void initBanner() {
        //初始化轮播图片数组
        LAUNCH_IMAGES.add(R.mipmap.launcher_01);
        LAUNCH_IMAGES.add(R.mipmap.launcher_02);
        LAUNCH_IMAGES.add(R.mipmap.launcher_03);
        LAUNCH_IMAGES.add(R.mipmap.launcher_04);
        LAUNCH_IMAGES.add(R.mipmap.launcher_05);

        //配置轮播图控件
        mConvenientBanner
                //设置轮播图片
                .setPages(new LauncherHolderCreator(), LAUNCH_IMAGES)
                //设置导航图片，框架中尽量用代码代替图片，减小框架体积
                .setPageIndicator(new int[]{R.drawable.dot_normal, R.drawable.dot_focus})
                //导航图片水平居中
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                //导航图片点击事件
                .setOnItemClickListener(this)
                //禁用自动轮播
                .setCanLoop(false);
    }

    //轮播图控件点击事件
    @Override
    public void onItemClick(int position) {
        //如果点击的是最后一个一张轮播图
        //在SP中记录已启动过App
        if (position == LAUNCH_IMAGES.size() - 1) {
            LattePreference.setAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name(), true);

            //检查用户是否已经登录
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.SIGNED);
                    }
                }

                @Override
                public void onNotSignIn() {
                    if (mILauncherListener != null) {
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
                    }
                }
            });
        }
    }

    //设置Fragment的布局
    //此页面布局就是一个控件轮播图控件
    @Override
    public Object setLayout() {
        mConvenientBanner = new ConvenientBanner<>(getContext());
        return mConvenientBanner;
    }

    //初始化函数
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        //初始化轮播图控件
        initBanner();
    }

    //获取启动页结束回调接口
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener) {
            mILauncherListener = (ILauncherListener) activity;
        }
    }
}
